package top.callback.inf.beautyleaf.server.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.callback.inf.beautyleaf.server.common.LeafResult;
import top.callback.inf.beautyleaf.server.model.PrefixModel;
import top.callback.inf.beautyleaf.server.service.PrefixService;

import java.util.List;

/**
 * 前缀接口
 *
 * @author Pengyu Gan
 * CreateDate 2021/11/13
 */
@Tag(name = "前缀接口", description = "前缀维护和角色绑定")
@RestController
@RequestMapping(value = "/api/prefix")
public class PrefixController {

    @Autowired
    private PrefixService prefixService;

    @Operation(summary = "新增前缀", parameters = {
            @Parameter(name = "prefixModel", description = "前缀信息模型", required = true)
    })
    @PostMapping(value = "/add")
    public LeafResult<Boolean> addPrefix(@RequestBody PrefixModel prefixModel) {
        boolean success = prefixService.addPrefix(prefixModel);
        return LeafResult.success(success);
    }

    @Operation(summary = "编辑前缀", parameters = {
            @Parameter(name = "prefixModel", description = "前缀信息模型", required = true)
    })
    @PostMapping(value = "/edit")
    public LeafResult<Boolean> editPrefix(@RequestBody PrefixModel prefixModel) {
        boolean success = prefixService.editPrefix(prefixModel);
        return LeafResult.success(success);
    }

    @Operation(summary = "删除前缀", parameters = {
            @Parameter(name = "prefixId", description = "前缀 ID", required = true)
    })
    @GetMapping(value = "/remove/{prefixId}")
    public LeafResult<Boolean> removePrefix(@PathVariable("prefixId") long prefixId) {
        boolean success = prefixService.removePrefix(prefixId);
        return LeafResult.success(success);
    }

    @Operation(summary = "查询指定前缀", parameters = {
            @Parameter(name = "prefixId", description = "前缀 ID", required = true)
    })
    @GetMapping(value = "/get/{prefixId}")
    public LeafResult<PrefixModel> findById(@PathVariable("prefixId") long prefixId) {
        PrefixModel prefixModel = prefixService.findById(prefixId);
        return LeafResult.success(prefixModel);
    }

    @Operation(summary = "查询所有前缀")
    @GetMapping(value = "/list")
    public LeafResult<List<PrefixModel>> findAll() {
        List<PrefixModel> prefixes = prefixService.findAll();
        return LeafResult.success(prefixes);
    }

}
